/******************************************************************************
* SVN­Version­Information:
* ­­­­­­­­­­­­­­­­­­­­­­­­---------------------
* $LastChangedRevision: 6 $
* $LastChangedBy: c.a.duevel $
* $LastChangedDate: 2009-08-26 15:12:37 +0000 (Wed, 26 Aug 2009) $
* $HeadURL: http://robofight.googlecode.com/svn/trunk/ClientJavaFX/src/de/hft_stuttgart/robofight/client/javafx/Register.fx $
******************************************************************************/

package de.hft_stuttgart.robofight.client.javafx;

import de.hft_stuttgart.robofight.client.java.JLogin;
import de.hft_stuttgart.robofight.client.java.JPlayer;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import de.hft_stuttgart.robofight.client.javafx.util.*;
import javafx.scene.control.TextBox;
import javafx.scene.Cursor;
import javafx.scene.control.Button;

import javafx.scene.control.Label;


/**
 * @author michael.rieker@web.de, ralf.dauenhauer@gmx.de
 */

public class Register extends CustomNode{

    package var getPlayer : function() : JPlayer;
    package var getNodeByName : function(nodeName : String) : Node;
    package var switchNode : function(className : String) : Void;
    public-init var jLogin : JLogin;
    package var getParam : function() : String;

    var loginSwing = false;
    var password = "";
    var passwordConfirm = "";
    var systemPassword1 = "12345";
    var systemPassword2 = "12345";



    public-read var username = TextBox {
        focusTraversable: true
        var fxzLayout = getNodeByName("regUsernameField").layoutBounds;
        layoutX: fxzLayout.minX + 7,
        layoutY: fxzLayout.minY + 7,
        font : Constants.FONT_TEXTFIELD,
        height : 24;
        width: 225
        skin: Constants.getTextBoxSkin()
    }

    var passwordField = SwingPasswordField{
        var fxzLayout = getNodeByName("regPasswordField").layoutBounds;
        layoutX: fxzLayout.minX + 12,
        layoutY: fxzLayout.minY + 8,
        font : Constants.FONT_TEXTFIELD,
        text: bind password with inverse
        action: function() {
             loginSwing = (password == systemPassword1);
        }
        foreground: Color.WHITE
    }

    var passwordFieldConfirm = SwingPasswordField{
        var fxzLayout = getNodeByName("regConfirmField").layoutBounds;
        layoutX: fxzLayout.minX + 12,
        layoutY: fxzLayout.minY + 8,
        font : Constants.FONT_TEXTFIELD,
        text: bind passwordConfirm with inverse
        action: function() {
             loginSwing = (passwordConfirm == systemPassword2);
        }
        foreground: Color.WHITE
    }

    var email = TextBox {
        var fxzLayout = getNodeByName("regEmailFieldLabel").layoutBounds;
        layoutX: fxzLayout.minX + 7,
        layoutY: fxzLayout.minY + 7,
        font : Constants.FONT_TEXTFIELD,
        height : 24
        width: 100
        skin: Constants.getTextBoxSkin();
    }

    var ATlabel = Label {
        var fxzLayout =  getNodeByName("regEmailFieldLabel").layoutBounds;
        layoutX: fxzLayout.minX + 105,
        layoutY: fxzLayout.minY +10,
        font : Constants.FONT_TEXTFIELD,
        text: "@"
        textFill: Color.WHITE
    }

    var email2 = TextBox {
        var fxzLayout =  getNodeByName("regEmailFieldLabel").layoutBounds;
        layoutX: fxzLayout.minX + 117,
        layoutY: fxzLayout.minY +7,
        font : Constants.FONT_TEXTFIELD,
        height : 24
        width: 100
        skin: Constants.getTextBoxSkin();
    }

    var saveButton : Button = Button {
        var fxzLayout = getNodeByName("regButtonSaveSettings").layoutBounds;
        layoutX : fxzLayout.minX
        layoutY : fxzLayout.minY
        text: "OK"
        action: function() {
             if(jLogin.checkInput({password} as String,
                        {passwordConfirm} as String)){
                            register();
             } else {
                    insert Message {
                            msg: "Passwords not equal! Please try again."
                            msgBoxBackground: getNodeByName("msgBoxmessageBox")

                            } into group.content;
                    password = "";
                    passwordConfirm = "";
             }
        }
        font: Constants.FONT_BUTTON
        cursor : Cursor.HAND
        width: 150
        height : 30
        skin: Constants.getButtonSkin()
    }

    var cancelButton : Button = Button {
        var fxzLayout = getNodeByName("regButtonCancel").layoutBounds;
        layoutX : fxzLayout.minX
        layoutY : fxzLayout.minY
        text: "cancel"
        action: function () {
            switchNode("Login");
        }
        font: Constants.FONT_BUTTON
        cursor : Cursor.HAND
        width: 150
        height : 30
        skin: Constants.getButtonSkin()
    }

    init {
        username.requestFocus();
        jLogin = new JLogin();
    }

    var group : Group = Group {
          content: [
            getNodeByName("background"),
            getNodeByName("logo_small"),
            getNodeByName("regLayoutFrame"),
            getNodeByName("regUsernameField"), 
            getNodeByName("regPasswordField"), 
            getNodeByName("regConfirmField"), 
            getNodeByName("regEmailFieldLabel"),
            getNodeByName("regUsernameLabel"),
            getNodeByName("regPasswordLabel"),
            getNodeByName("regConfirmLabel"),
            getNodeByName("regEmailLabel"),
            username,
            passwordField,
            passwordFieldConfirm,
            email,
            ATlabel,
            email2,
            saveButton,
            cancelButton
         ]
    }

    override function create() : Node{
       return group;
    }

    function register(){
        var tmp : String;
        jLogin = new JLogin();
        jLogin.setLocal(getParam());
        if((tmp = jLogin.connect()) == null){
            tmp = jLogin.register({username.text} as String,{password} as String, concatEmailFields());
            if(tmp==""){
                switchNode("Login");
            } else {
                insert Message {
                    msg: tmp;
                    msgBoxBackground: getNodeByName("msgBoxmessageBox")
                } into group.content;
            }
        } else {
        insert Message {
                msg: tmp
                msgBoxBackground: getNodeByName("msgBoxmessageBox")

                } into group.content;
        }
    }

    function concatEmailFields() : String{
        return "{email.text}@{email2.text}"
    }

}
